<template>
    <div class="edit-popup">
        <popup ref="popupRef" :title="popupTitle" :async="true" width="550px" @confirm="handleSubmit"
            @close="handleClose">
            <el-form class="ls-form" ref="formRef" :rules="rules" :model="formData" label-width="60px">
                <el-form-item label="名称" prop="name">
                    <el-input class="ls-input" v-model="formData.name" placeholder="请输入名称" clearable />
                </el-form-item>
                <el-form-item label="级别" prop="grade">
                    <el-select v-model="formData.grade" placeholder="请选择" style="width: 240px">
                        <el-option v-for="item in optionsData" :key="item.value" :label="item.label"
                            :value="item.value" />
                    </el-select>
                </el-form-item>
                <el-form-item label="备注" prop="desc">
                    <el-input v-model="formData.desc" type="textarea" :autosize="{ minRows: 4, maxRows: 6 }"
                        placeholder="请输入备注" maxlength="200" show-word-limit />
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <el-input-number v-model="formData.sort" :min="0" :max="9999" />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>
<script lang="ts" setup>
import type { FormInstance } from 'element-plus'

import { roleAdd, roleEdit } from '@/api/perms/role'
import Popup from '@/components/popup/index.vue'

const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑角色' : '新增角色'
})
const optionsData = [
    {
        value: '0',
        label: '无',
    },
    {
        value: '1',
        label: '省份',
    },
    {
        value: '2',
        label: '城市',
    },
    {
        value: '3',
        label: '所属区/县',
    },
]
const formData = reactive({
    id: '',
    name: '',
    desc: '',
    sort: 0,
    grade: "",
    menu_id: []
})

const rules = {
    name: [
        {
            required: true,
            message: '请输入名称',
            trigger: ['blur']
        }
    ],
    grade: [
        {
            required: true,
            message: '请选择级别',
            trigger: ['change']
        }
    ]
}

const handleSubmit = async () => {
    await formRef.value?.validate()
    mode.value == 'edit' ? await roleEdit(formData) : await roleAdd(formData)
    popupRef.value?.close()
    emit('success')
}

const handleClose = () => {
    emit('close')
}

const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            // 确保grade字段转换为字符串类型，以匹配optionsData中的值类型
            formData[key] = key === 'grade' ? String(data[key]) : data[key]
        }
    }
}

defineExpose({
    open,
    setFormData
})
</script>
